package org.wlgzs.office_exam_student;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.*;

import com.songminju.jdagger.JDEncode;
import com.songminju.jdagger.JDString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wlgzs.office_exam_student.dao.BaseDao;
import org.wlgzs.office_exam_student.dao.MyQueryRunner;
import org.wlgzs.office_exam_student.entity.*;
import org.wlgzs.office_exam_student.service.ChoiceQuestionService;
import org.wlgzs.office_exam_student.service.OperateQuestionService;
import org.wlgzs.office_exam_student.service.ScoreService;
import org.wlgzs.office_exam_student.service.UserService;
import org.wlgzs.office_exam_student.view.View;
import org.wlgzs.office_exam_student.view.ViewFactory;
import org.wlgzs.office_exam_student.view.freemarker.FMViewFactory;


public class Test {
	
	static Logger logger = LoggerFactory.getLogger(Test.class);
	static ViewFactory vf = new FMViewFactory("/home/song/temp/");
	static OperateQuestionService oqs = new OperateQuestionService();
	static List<OperateQuestion> all = oqs.queryAll();
	public static void main(String[] args) throws Exception {
		ScoreService ss = new ScoreService();
		ss.insert(new Score());
	}


	public static void createCQ(){
		ChoiceQuestionService cqs = new ChoiceQuestionService();
		List<ChoiceQuestion> cs1 = cqs.choiceQuestions(1,30,ChoiceQuestion.GRADE_ONE);

		List<ChoiceQuestion> cs2 = cqs.choiceQuestions(1,30,ChoiceQuestion.GRADE_TWO);

		String sql =
"insert into tb_choice_question (grade,title,option_a,option_b,option_c,option_d,answer,analysis,school_id,school,score)values(?,?,?,?,?,?,?,?,?,?,?)";
		for(int i=0;i<5;i++){
			for(ChoiceQuestion c:cs1){

				c.setSchoolId(220 + i);
				c.setSchool("S"+(i+1));
				cqs.exec(new BaseDao.SqlTask<ChoiceQuestion>() {

					@Override
					public ChoiceQuestion exec(MyQueryRunner qr) {
						try {
							qr.execute(sql,c.getGrade(),c.getTitle(),c.getOptionA(),c.getOptionB(),c.getOptionC(),c.getOptionD(),
									c.getAnswer(),c.getAnalysis(),c.getSchoolId(),c.getSchool(),c.getScore());
						} catch (SQLException e) {
							e.printStackTrace();
						}
						return null;
					}
				});
			}

		}







	}


	
	public static void renderQuestion() throws Exception {
		File file = new File("/home/song/temp/questions");
		
		
		for(File f:file.listFiles()) {
			String name = f.getName();
			name = name.substring(0,name.length()-4);
			Quest q = null;
			BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
			String str = reader.readLine();
			char li = 1;
			while(str != null) {
				logger.debug(str);
				if(str.length() == 0) {
					str = reader.readLine();
					continue;
				}
				if(str.equals("字处理题")) {
					li = 1;
					q = new Quest();
					q.key = name.substring(0,3) + "_word_"+name.charAt(3);
					String s = reader.readLine();
					char c = s.charAt(0);
					if(c>'0' && c<= '9') {
						if(c == li  &&  s.charAt(1)=='.') {
							q.sub.add("<div class=\"question-sub-2\">"+s+"</div>");
						}else {
							q.sub.add("<div class=\"question-sub-1\">"+s+"</div>");
							li = c;
						}
						
					}else {
						q.title = s;
					}
				}else if(str.equals("表格处理题")) {
					li = 1;
					print(q);
					q = new Quest();
					q.key = name.substring(0,3) + "_excel_"+name.charAt(3);
					String s = reader.readLine();
					char c = s.charAt(0);
					if(c>'0' && c<= '9'  &&  s.charAt(1)=='.') {
						if(c == li) {
							q.sub.add("<div class=\"question-sub-2\">"+s+"</div>");
						}else {
							q.sub.add("<div class=\"question-sub-1\">"+s+"</div>");
							li = c;
						}
					}else {
						q.title = s;
					}
				}else if(str.equals("演示文稿题")) {
					li = 1;
					print(q);
					q = new Quest();
					q.key = name.substring(0,3) + "_ppt_"+name.charAt(3);
					String s = reader.readLine();
					char c = s.charAt(0);
					if(c>'0' && c<= '9') {
						if(c == li &&  s.charAt(1)=='.') {
							q.sub.add("<div class=\"question-sub-2\">"+s+"</div>");
						}else {
							q.sub.add("<div class=\"question-sub-1\">"+s+"</div>");
							li = c;
						}
					}else {
						q.title = s;
					}
				}else {
					
					char x = str.charAt(0);
					if(x > '0' && x <= '9'  &&  str.charAt(1)=='.') {
						if(x == li) {
							q.sub.add("<div class=\"question-sub-2\">"+str+"</div>");
						}else {
							q.sub.add("<div class=\"question-sub-1\">"+str+"</div>");
							li = x;
						}
					}else {
						q.sub.add("<div class=\"question-sub-2\">"+str+"</div>");
						li = x;
					}
				}
				str = reader.readLine();
			}
			print(q);
		}
	}
	public static void print(Quest q) {
		View v = vf.getView("q.ftl");
		Map<String,Object> data = new HashMap<>();
		data.put("title",q.title);
		data.put("sub", q.sub);
		try {
			StringWriter out = new StringWriter();
			v.render(data,out);
			System.out.println(out);
			for(OperateQuestion oq:all) {
				if(q.key.equals(oq.getSourceUrl())) {
					oq.setTitle(out.toString());
					oqs.update(oq);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
class Quest {
	String key = "";
	String title = "";
	List<String> sub = new LinkedList<>();
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(key).append("\n");
		sb.append(title).append("\n");
		for(String s:sub)
			sb.append(s).append("\n");
		return sb.toString();
	}
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public List<String> getSub() {
		return sub;
	}
	public void setSub(List<String> sub) {
		this.sub = sub;
	}
}